package com.hongyuan.sellfitness.ui.person.view_mode;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.base.ItemViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.ui.find.beans.FindChildTopBean;

import java.util.List;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class FansViewModel extends CustomViewModel<MyRepository> {

    public FansViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<FansItemViewModel> observableList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<FansItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_fans);

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //弹框提示
        public SingleLiveEvent<Void> showCancel = new SingleLiveEvent<>();
    }

    /*
    * 取消关注
    * */
    public void attention(){
        if(itemViewModel != null){
            setParams("f_mid",String.valueOf(itemViewModel.entity.get().getM_id())).setParams("f_type","add");
            requestData(Constants.ADD_FRIENT);
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        requestData(Constants.GET_GZ_MY);
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_GZ_MY){
            List<FindChildTopBean.ListBean> mList = ((FindChildTopBean)dataBean).getList();
            //清除旧数据
            if(curPage == FIRST_PAGE){
                observableList.clear();
            }

            if(mList != null && mList.size() > 0){
                for(FindChildTopBean.ListBean bean : mList){
                    FansItemViewModel itemViewModel = new FansItemViewModel(this,bean);
                    observableList.add(itemViewModel);
                }
            }

            if(observableList.size() > 0){
                if(mList == null || mList.size() == 0){
                    getUC().getFinishLoadMoreData().call();
                }
            }else{

            }
        }

        if(code == Constants.ADD_FRIENT){
            itemViewModel.entity.get().setIs_xh(1);
            itemViewModel.entity.notifyChange();
        }
    }

    //当前需要关注的对象
    public FansItemViewModel itemViewModel;

    @Override
    public void itemClick(ItemViewModel itemVM) {
        super.itemClick(itemVM);
        this.itemViewModel = (FansItemViewModel) itemVM;
    }
}
